<?php
namespace app\api\controller\income;

use app\common\controller\Api;
use think\Request;
use think\Cache;
use think\Db;
use think\Log;
use app\api\model\wanlshop\AccIncome;
use app\api\model\wanlshop\InvestApplyFor;
class Income extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight='*';

    //获取我的收益列表
    public function list(Request $request){
        $id = $request->param('user_id');
        // 每页显示的记录数
        $pageSize = 20;
        $page = 1;
        $result = Db::table('fa_accumulated_income')
            ->where('user_id', $id)
            ->alias('i')
            ->field('i.goods_id, SUM(i.revenue) as total_revenue, COUNT(*) as num, g.*')
            ->join('wanlshop_goods g', 'i.goods_id = g.id')
            ->group('i.goods_id')
            ->paginate($pageSize, false, ['page' => $page]);
        return $this->success('返回成功', $result);
    }
    //获取我的再投资金总额
    public function all_reinvestment(Request $request){
        $userId = $request->param('user_id');
        // 执行查询
        $result = Db::table('fa_invest_apply_for')
            ->where('user_id', $userId)
            ->field('SUM(price) as total_price')
            ->find();

        // 提取总价格
        $totalPrice = $result['total_price'];
        return $this->success('返回成功', $totalPrice);
    }
    //获取我的再投资金列表
    public function re_list(Request $request){
        $id = $request->param('user_id');
        // 每页显示的记录数
        $pageSize = 20;
        $page = 1;
        $InvestApplyFor = new InvestApplyFor();
        $subQuery = Db::name('accumulated_income')
            ->field('SUM(revenue) as totalRevenue')
            ->where('apply_id', Db::raw('fa_invest_apply_for.id'))
            ->buildSql();
        $result = $InvestApplyFor->field('goods_id, COUNT(*) as num')->where('user_id',$id)->where('status','through')
            ->field($subQuery . ' as revenueTotal')
            ->with(['incomes','good'])
            ->paginate($pageSize, false, ['page' => $page]);
        return $this->success('返回成功', $result);
    }
    //获取邀请人列表
    public function user(Request $request){
        $id = $request->param('user_id');
        // 每页显示的记录数
        $pageSize = 20;
        $page = 1;
        $result = Db::name('user')
            ->where('referrer',$id)
            ->paginate($pageSize, false, ['page' => $page]);
        return $this->success('返回成功', $result);
    }
}